GameplayAwardSystem框架

随机决策与领域写入具有不同的成功条件。随机表输出确定的 Key 与数量,领域适配器根据当前约束执行请求,并返回实际提交量。两层分别保存结果,避免把抽取成功等同于写入成功。

GameplayAwardSystem 将随机抽取和实际发放分层。抽取输出无业务语义的 Key 与数量,发放层把业务请求交给适配器,并汇总实际成功结果。

基础架构

图中按职责分组列出核心类;+、-、# 表示公开、私有和受保护成员。字段类型保留容器与指针语义,TMap 的键和值用 | 分隔,分组不代表额外的源码命名空间。

%%{init: {"class": {"useMaxWidth": false}, "flowchart": {"useMaxWidth": false}}}%%
classDiagram
    direction LR

    namespace Roll {
        class UGameplayAwardRollManager
        class FGameplayAwardRollTable
        class FGameplayAwardRollEntry
    }

    namespace Commit {
        class UGameplayAwardManager
        class UGameplayAwardComponent
    }

    namespace Result {
        class FGameplayAwardThingInfo
    }

    UGameplayAwardRollManager *-- FGameplayAwardRollTable : TableRegistry
    FGameplayAwardRollTable *-- FGameplayAwardRollEntry : Entries
    UGameplayAwardManager --> UGameplayAwardComponent : 弱引用登记
    UGameplayAwardManager ..> FGameplayAwardThingInfo : 逐项提交
    UGameplayAwardComponent ..> FGameplayAwardThingInfo : 结果通知

    class UGameplayAwardRollManager {
        - TableRegistry : TMap~FString|FGameplayAwardRollTable~
        - MaxDepth : int32
        - RollByKey(Key, ExcludeKeys) TMap~FString|int32~
        - RollInternal(TableKey, Table, VisitedKeys, ExcludeKeys, Depth) TMap~FString|int32~
        - WeightedSelect(Entries, ExcludeKeys)
    }

    class FGameplayAwardRollTable {
        + Entries : TArray~FGameplayAwardRollEntry~
        + CountMin : int32
        + CountMax : int32
        + bWithReplacement : bool
        + bRecursive : bool
    }

    class FGameplayAwardRollEntry {
        + Key : FString
        + Weight : float
    }

    class UGameplayAwardManager {
        # RegisteredComponents : TMap~uint64|TWeakObjectPtr~UGameplayAwardComponent~~
        # DefaultAwardConfigs : FGameplayAwardConfigs
        # OnAddItem(PlayerUID, OutThingInfo, Params)
        # OnAddItemFinished(PlayerUID, ThingInfos, Params)
    }

    class UGameplayAwardComponent {
        + GetUID() uint64
        + PrepareAwardParams(ThingInfo, InOutParams)
        + NotifyGetAwards(AwardItemInfos, Params)
    }

    class FGameplayAwardThingInfo {
        + ItemID : int32
        + Count : int32
        + ItemUID : uint64
        + GradeType : int32
        + Params : FCommonVariantParams
    }

RollManager 在 World 初始化时加载并注册表,强持有配置来源。AwardManager 注册持有者组件,按条调用业务发放入口;组件负责接收者信息、参数策略与通知。随机表不依赖某一种背包。

随机配置与提交结果

随机表只输出键与数量,领域提交再转换为实际结果。配置中的概率、轮数与递归策略都有独立字段:

1
2
3
4
5
struct FGameplayAwardRollEntry
{
FString Key;
float Weight = 1.0f;
};
1
2
3
4
5
6
7
8
struct FGameplayAwardRollTable
{
TArray<FGameplayAwardRollEntry> Entries;
int32 CountMin = 1;
int32 CountMax = 1;
bool bWithReplacement = true;
bool bRecursive = true;
};
1
2
3
4
5
6
7
8
struct FGameplayAwardThingInfo
{
int32 ItemID = 0;
int32 Count = 0;
uint64 ItemUID = 0;
int32 GradeType = 0;
FCommonVariantParams Params;
};

CountMin 与 CountMax 限定抽取次数,默认均为 1;bWithReplacement 决定已选候选是否仍参与本次表的后续抽取,bRecursive 决定选中的键是否继续作为表键展开。Weight 默认 1.0,有效权重集合同时用于求和和选择,过滤规则必须一致。

ItemID 表示定义,ItemUID 表示实际实例,Count 描述条目的数量语义。随机阶段没有承诺资源已创建;提交层的扩展回调可修改结果条目,最终通知应消费实际输出。Params 携带通用参数,不应把随机命中次数直接解释成成功提交数量。

随机表的数据模型

Roll 只处理 Key 与数量,不依赖接收者或最终存储。TableKey 标识随机表,Entry.Key 可以表示叶子或另一张已注册表,递归策略决定如何解释。

定义字段 算法职责
CountMin / CountMax 决定当前表的抽取次数范围
Entries 按配置顺序保存 Key 与 Weight
bWithReplacement 决定命中后是否从本次候选副本移除同 Key 项
bRecursive 决定命中 Key 是否继续解释为子表

抽取结果使用 TMap<FString, int32> 累加叶子数量。领域提交结果单独报告实际数量,不由随机表推断。

次数属于当前表的抽取次数,最终叶子数量由递归与累加形成。跨资产 TableKey 应唯一;引用名称错误可能被解释成叶子,因此引用图校验与领域叶子校验是不同职责。

加权抽取与递归

对正权重候选,总权重为 W = Σ wi,候选概率为 wi / W。在 [0, W) 中取随机值,按累积权重找到落点。

有放回抽取保留候选,无放回从本次表的工作副本移除所有同 Key 条目。这只保证当前表内的移除语义,同一叶子仍可能由不同子表产出,最终 Map 会累加数量。

递归用当前调用路径检测环,而非全局 visited 集合。多个父表引用同一子表是合法复用;只有再次进入当前展开路径中的表才形成环。根深度为 0,到达深度 8 返回空结果。

叶子排除集合向子表传递,过滤最终 Key,不用于阻止展开指定子表。配置应保证非空 Key、非负且有序的次数范围和正权重;当前算法不会自动修正负权重。

公开接口的语义也不同:

接口 递归与次数
RollByKey 查询世界注册表,按表控制递归与次数
Roll 直接对传入表抽取,不递归
RollOnce 只抽一次,不读取次数范围,也不递归

随机使用 FMath,接口没有独立 Seed 或 RandomStream。检查分布要做足量采样,不能要求不同机器按同一个种子复现完全相同的序列。

递归展开与局部候选集

当前路径集合用于拒绝递归环,局部候选副本用于表达无放回。两者都属于一次抽取过程,不写回共享表定义。

递归过程保存当前路径的 VisitedKeys,以及当前表独立的候选数组:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
TMap<FString, int32> UGameplayAwardRollManager::RollInternal(const FString& TableKey,
const FGameplayAwardRollTable& Table,
TSet<FString>& VisitedKeys,
const TSet<FString>& ExcludeKeys,
int32 Depth) const
{
if (Depth >= MaxDepth)
{
return {};
}

if (VisitedKeys.Contains(TableKey))
{
return {};
}

VisitedKeys.Add(TableKey);
TMap<FString, int32> Results;

const int32 Count = FMath::RandRange(Table.CountMin, Table.CountMax);

TArray<FGameplayAwardRollEntry> AvailableEntries = Table.Entries;

for (int32 i = 0; i < Count; ++i)
{
const FGameplayAwardRollEntry* Entry = WeightedSelect(AvailableEntries, ExcludeKeys);
if (!Entry)
{
break;
}

const FGameplayAwardRollTable* SubTable = Table.bRecursive ? GetTable(Entry->Key) : nullptr;

if (SubTable)
{
auto SubResults =
RollInternal(Entry->Key, *SubTable, VisitedKeys, ExcludeKeys, Depth + 1);
for (const auto& [LeafKey, Cnt] : SubResults)
{
Results.FindOrAdd(LeafKey) += Cnt;
}
}
else
{
if (Entry->Key.IsEmpty())
{
continue;
}

if (ExcludeKeys.Contains(Entry->Key))
{
continue;
}

Results.FindOrAdd(Entry->Key) += 1;
}

if (!Table.bWithReplacement)
{
const FString SelectedKey = Entry->Key;
AvailableEntries.RemoveAll([&SelectedKey](const FGameplayAwardRollEntry& E)
{ return E.Key == SelectedKey; });
}
}

VisitedKeys.Remove(TableKey);
return Results;
}

VisitedKeys 在进入时添加、正常退出时移除,用来阻止当前递归链成环;它不是跨所有分支永久去重。AvailableEntries 是本次表调用的副本,非放回模式只改该副本。删除候选前先复制 SelectedKey,避免 RemoveAll 使 Entry 指针失效后继续读取。

路径集合在返回时移除当前表,因此不同分支可以共享同一子表。无放回只约束当前表候选,同名叶子仍可从多个子表累加。WeightedSelect 负责叶子排除与权重选择,表展开逻辑不承担实际发放。

递归返回的是叶子计数 Map,父层只做累加,不将子结果数量再乘一次。当前表的 Count 决定展开多少次,子表的 Count 决定每次展开的叶子数量。无放回限制本层选择,不会自动变成整棵结果树的全局去重。

从抽取结果到实际产出

适配层把叶子 Key 解析为领域请求,补充接收者、数量与参数。随机表引用和领域定义采用独立的配置契约,转换发生在适配边界,抽取算法不负责选择目标容器或创建实体。

实际发放顺序为:

  1. 验证接收者与正数量请求;
  2. 为当前 ThingInfo 复制请求 Params;
  3. 通过持有者组件 PrepareAwardParams 应用逐条目标写入策略;
  4. 调用 OnAddItem 完成业务发放;
  5. 将 Count 回写为实际成功数量,失败为 0;
  6. 收集 Count 大于 0 的条目,执行完成回调与通知。

参数副本避免某条产出的目标容器策略污染下一条。准备钩子仅在权威端且 Owner 是有效 PlayerState 时执行,不依赖客户端复制参数。

Manager 会把请求 Count 至少修正为 1,因此调用方必须提前过滤非正数量;不能用 Count=0 表示跳过。

结果与提交边界

随机结果转换为领域请求后,适配器验证接收者、数量和当前资源约束,并报告实际提交量。框架只累计实际正数量的结果再发完成通知。这里保留接口契约,具体领域适配实现由其状态所有者提供。

返回结果记录适配器报告的实际提交数量。完成通知与显示反馈只消费该结果,不充当写入凭据;上游是否继续推进,由明确的结果契约决定。

AwardManager 逐项执行,不提供整份请求的统一回滚。单条多件是否全部成功,也由发放器决定。如果任务要求全部产出一起提交,应在适配层先准备资源并定义失败撤销,而不是把循环调用 Award 当作事务。

随机决策与领域写入的边界

RollResult 是已经完成随机决策的纯数据;AwardRequest 是领域写入意图;AwardResult 是实际写入结果。三者分开保存,使重试可以围绕确定的请求执行,避免发放路径重新解释随机表。

领域适配器负责解析 Key、选择目标存储并报告成功数量。Manager 逐条推进,不提供整份请求的跨领域回滚;需要整体提交时,准备与撤销协议应属于对应领域,而不增加到权重抽取算法中。

加权选择的候选集合与边界

递归展开只负责决定何时调用加权选择,以及命中后是否移除候选。WeightedSelect 负责计算可参与条目的总权重、选择落点并处理边界,完整方法如下。

总权重与选择循环使用同一过滤条件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const FGameplayAwardRollEntry* UGameplayAwardRollManager::WeightedSelect(
const TArray<FGameplayAwardRollEntry>& Entries, const TSet<FString>& ExcludeKeys) const
{
if (Entries.IsEmpty())
{
return nullptr;
}

float TotalWeight = 0.0f;
for (const auto& Entry : Entries)
{
const bool bIsLeaf = !HasTable(Entry.Key);
if (bIsLeaf && ExcludeKeys.Contains(Entry.Key))
{
continue;
}

TotalWeight += Entry.Weight;
}

if (TotalWeight <= 0.0f)
{
return nullptr;
}

float RandomValue = FMath::FRandRange(0.0f, TotalWeight);

for (const auto& Entry : Entries)
{
const bool bIsLeaf = !HasTable(Entry.Key);
if (bIsLeaf && ExcludeKeys.Contains(Entry.Key))
{
continue;
}

RandomValue -= Entry.Weight;
if (RandomValue <= 0.0f)
{
return &Entry;
}
}

for (int32 i = Entries.Num() - 1; i >= 0; --i)
{
const auto& Entry = Entries[i];
const bool bIsLeaf = !HasTable(Entry.Key);
if (bIsLeaf && ExcludeKeys.Contains(Entry.Key))
{
continue;
}
return &Entry;
}

return nullptr;
}

两轮遍历必须使用一致的排除规则,否则随机区间和实际候选不对应。这里以 HasTable 判断当前注册表中是否存在子表:非表 Key 且在 ExcludeKeys 中才跳过。总权重非正时没有结果;有效权重依配置预先保证,方法没有把负值逐项修正为零。

表查找与注册表身份

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void UGameplayAwardRollManager::RegisterTable(const FString& Key,
const FGameplayAwardRollTable& Table)
{
if (Key.IsEmpty())
{
return;
}

TableRegistry.Add(Key, Table);
}

const FGameplayAwardRollTable* UGameplayAwardRollManager::GetTable(const FString& Key) const
{
return TableRegistry.Find(Key);
}

bool UGameplayAwardRollManager::HasTable(const FString& Key) const
{
return TableRegistry.Contains(Key);
}

空注册 Key 被拒绝,同 Key 注册使用 Map.Add 替换定义。HasTable 同时参与递归解释和选择阶段的叶子判定,所以注册表改变不仅影响查找,也影响某个 Key 的语义。尤其 bRecursive=false 时,最终仍按叶子消费命中 Key,而 WeightedSelect 的初始过滤使用注册表身份;展开层保留最终叶子排除检查,用来维护两层语义的边界。